package com.czwj.realm;

import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.czwj.entities.UserInfo;
import com.czwj.service.UserInfoService;

public class UserRealm extends AuthorizingRealm {

	@Resource
	UserInfoService userInfoService;
	
	@Autowired
    private SessionDAO sessionDAO;
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// TODO Auto-generated method stub
		String username = (String) principals.fromRealm(getName()).iterator().next();
		UserInfo user = userInfoService.findByUserName(username);
		if (user == null)
			return null;
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		List<Role> roles = user.getRoles();
//		if(roles !=null)
//		{
//			for (Role role : roles) {
//				List<Permission> permissions = role.getPermissions();
//				info.addRole(role.getName());
//				for (Permission permission : permissions) {
//					info.addStringPermission(permission.getName());
//				}
//			}
//		}
//		for (Permission per : user.getPermissions())
//			info.addStringPermission(per.getName());
		return info;
	}

	@Override					
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		UsernamePasswordToken utoken = (UsernamePasswordToken) token;
        String username = utoken.getUsername();
//        Collection<Session> sessions = sessionDAO.getActiveSessions();
//        if(sessions.size()>0) {
//            for (Session session : sessions) {
//                System.out.println("::::::::::::::::" + session);
//                //获得session中已经登录用户的名字
//                if(null!=session.getAttribute("userInfo")) {
//                    String loginUsername = ((Userinfo)session.getAttribute("userInfo")).getUsername();
//                    System.out.println("::::::::::::::::" + loginUsername);
//                    if (username.equals(loginUsername)) {  //这里的username也就是当前登录的username
//                        session.stop();  //这里就把session清除，
//                        System.out.println("清除了");
//                    }
//                }
//            }
//        }
        UserInfo user = userInfoService.findByUserName(username);
		if (user == null)
			return null;
		return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
		
	}

}
